
package com.kanjian.music.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.kanjian.music.R;
import com.kanjian.music.activity.AboutUsActivity;
import com.kanjian.music.activity.FeedbackActivity;
import com.kanjian.music.activity.LoginActivity;
import com.kanjian.music.api.ApiRequestManager;
import com.kanjian.music.constants.IntentConstants;
import com.kanjian.music.entity.BaseCode;
import com.kanjian.music.entity.User;
import com.kanjian.music.network.HttpLoader;

/**
 * 更多
 * 
 * @author xuyanjun@kanjian.com
 */
public class MoreFragment extends BaseFragment implements LoaderCallbacks<String> {

    View mRootView;

    Button mBtn_feedback;

    Button mBtn_mark;

    private Button mBtn_aboutus;

    private Button mBtn_loginStatus;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mRootView == null) {
            initView(inflater, container);
        }
        return mRootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mRootView = null;
        getLoaderManager().destroyLoader(0);
    }
    
    @Override
    public void onResume() {
        super.onResume();
        checkLogin();
    }

    private void checkLogin() {
        if (User.isLogin()) {
            mBtn_loginStatus.setText("注销" + User.getCurrentUser().getUserName());
        } else {
            mBtn_loginStatus.setText("登陆");
        }
    }

    private void initView(LayoutInflater inflater, ViewGroup container) {
        mRootView = inflater.inflate(R.layout.fragment_more, container, false);
        mBtn_feedback = (Button) mRootView.findViewById(R.id.preference_btn_feedback);
        mBtn_mark = (Button) mRootView.findViewById(R.id.preference_btn_mark);
        mBtn_aboutus = (Button) mRootView.findViewById(R.id.preference_btn_about_us);
        mBtn_loginStatus = (Button) mRootView.findViewById(R.id.preference_btn_login_status);
        setClickListener();
        checkLogin();
    }

    private void setClickListener() {
        mBtn_feedback.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), FeedbackActivity.class));
            }
        });
        mBtn_mark.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String mAddress = "market://details?id=" + getActivity().getPackageName();
                Intent marketIntent = new Intent("android.intent.action.VIEW");
                marketIntent.setData(Uri.parse(mAddress));
                try {
                    startActivity(marketIntent);
                } catch (Exception e) {
                    // 未安装应用市场
                }
            }
        });
        mBtn_aboutus.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), AboutUsActivity.class));
            }
        });
        mBtn_loginStatus.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (User.isLogin()) {
                    Bundle b = new Bundle();
                    b.putSerializable(IntentConstants.HTTPLOADERREQUESTPARAM,
                            ApiRequestManager.getLogoutRequest());
                    getLoaderManager().initLoader(0, b, MoreFragment.this);
                } else {
                    startActivityForResult(new Intent(getActivity(), LoginActivity.class), 0);
                }
            }
        });
    }

    @Override
    public Loader<String> onCreateLoader(int id, Bundle args) {
        return new HttpLoader(getActivity(), args);
    }

    @Override
    public void onLoadFinished(Loader<String> loader, String data) {
        if (data != null) {
            BaseCode code = BaseCode.getCommonCodeFromString(data);
            if (code != null && code.mErrorCode == 20000) {
                Toast.makeText(getActivity(), data, 1).show();
                User.localLogout();
            }
        }
    }

    @Override
    public void onLoaderReset(Loader<String> loader) {
    }

}
